var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    service:null,
    mealDtlList:[],
    serviceMeal:'',
    type:'',
    id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    wx.showLoading({
      title: '加载中...',
    })

    var that = this;
    that.setData({
      type: options.type,
      id:options.id,
    })
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          source: res.data.source
        });
        util.getReq("/app/service/toBuyService", {
          source: res.data.source,
          uid: res.data.user_id,
          id:options.id,
          'type':options.type
        }, function (datas) {
          wx.hideLoading();
          if (datas.code == 0) {
            if (options.type==1){
                let mealDtlList = datas.params.mealDtlList;


                let serviceMealList = datas.params.serviceMeal;
                let array=[];
                for (let i = 0; i < mealDtlList.length;i++){
                    array.push({
                      'index':i+1,
                      service_name: mealDtlList[i].service_name,
                      service_type: mealDtlList[i].service_type,
                      service_num: mealDtlList[i].service_num,
                      use_term: mealDtlList[i].use_term,
                      service_price: mealDtlList[i].service_price,

                    })
                  }
                that.setData({
                  mealDtlList: array,
                  serviceMeal: serviceMealList
                })
              }else{
                let serviceList = datas.params.serviceMeal
                  that.setData({
                    service: serviceList
                  })
              }
            
          } else {
            var errMsg = '服务器异常';
            if (datas.desc) {
              errMsg = datas.desc;
            }
            wx.showToast({
              title: errMsg,
              icon:'none',
            })
          }

        })
      }
    })  
  },
/**
 * 杉德支付调起
 */
  nagtiveToPay:function(openid){
    var that=this;
      wx.getStorage({
        key: 'userInfo',
        success: function (res) {
          
          util.getReq("/app/service/toPay", {
            source: res.data.source,
            uid: res.data.user_id,
            'type': that.data.type,
            'id': that.data.id,
            'openid': openid
          }, function (datas) {
            wx.hideLoading();
            if (datas.code == 0) {
              var data = datas.data;
              console.log(data);
              var returnres = JSON.parse(data);
              if (returnres.head.respCode != "000000")  {
                wx.showToast({
                  title: "服务器异常",
                  icon: 'none',
                })
              }else{
                var credentail = JSON.parse(returnres.body.credential);
                var payInfo = JSON.parse(credentail.params);
                  wx.requestPayment({
                    timeStamp: payInfo.timeStamp,
                    nonceStr: payInfo.nonceStr,
                    package: payInfo.package,
                    signType: payInfo.signType,
                    paySign: payInfo.paySign,
                    'success': function (res) {
                      wx.showToast({
                        title: "支付成功",
                        icon: 'none',
                      })
                      wx.redirectTo({
                        url: '/pages/my/service/service_list',
                      })
                    },
                    'fail': function (res) {
                      wx.showModal({
                        title: '支付信息',
                        content: res,
                      })
                      wx.showToast({
                        title: "支付失败",
                        icon: 'none',
                      })
                    },
                    
                  })
              }

            } else {
              var errMsg = '服务器异常';
              if (datas.desc) {
                errMsg = datas.desc;
              }
              wx.showToast({
                title: errMsg,
                icon: 'none',
              })
            }

          })
        }
      })  
    
  },
  onGotUserInfo:function(e){
    var that=this;
    wx.login({
      success: res => {
        //console.log(res.code)
        //发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          util.getReq("/app/service/getOpenId", {
            'code': res.code,
            source: that.data.source,
          }, function (datas) {
            wx.hideLoading();
            if (datas.code == 0) {
              that.setData({
                openId: datas.data,
              })
              that.nagtiveToPaywx(datas.data);
              //that.nagtiveToPaywx(datas.data);
            } else {
              var errMsg = '服务器异常';
              if (datas.desc) {
                errMsg = datas.desc;
              }
              wx.showToast({
                title: errMsg,
                icon: 'none',
              })
            }

          })
        }
      }
    })
 
  },
  /**
   * 微信支付
   */
  nagtiveToPaywx:function(openid){
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {

        util.getReq("/app/service/toPayTX", {
          source: res.data.source,
          uid: res.data.user_id,
          'type': that.data.type,
          'id': that.data.id,
          'openid': openid
        }, function (datas) {
          wx.hideLoading();
          if (datas.code == 0) {
            var data = datas.data;
            console.log(data);
            var returnres = data;
            if (returnres.return_code != "SUCCESS") {
              wx.showToast({
                title: "服务器异常",
                icon: 'none',
              })
            } else {
              var payInfo = returnres;
              console.log(payInfo.paySign);
              wx.requestPayment({
                "appId": payInfo.appid, //公众号名称，由商户传入
                "timeStamp": payInfo.timestamp, //戳，自1970 年以来的秒数
                "nonceStr": payInfo.noncestr, //随机串
                "package": payInfo.package,
                "signType": payInfo.sign_type, //微信签名方式:
                "paySign": payInfo.paySign,  //微信签名,
                'success': function (res) {
                  wx.showToast({
                    title: "支付成功",
                    icon: 'none',
                  })
                  wx.redirectTo({
                    url: '/pages/my/service/service_list',
                  })
                },
                'fail': function (res) {
                  wx.showModal({
                    title: '支付信息',
                    content: res,
                  })
                  wx.showToast({
                    title: "支付失败",
                    icon: 'none',
                  })
                },

              })
            }

          } else {
            var errMsg = '服务器异常';
            if (datas.desc) {
              errMsg = datas.desc;
            }
            wx.showToast({
              title: errMsg,
              icon: 'none',
            })
          }

        })
      }
    })  
  }
})